include:
  - git.cijs_key

Add group permission to /home/lighttpd:
  file.directory:
    - name: /home/lighttpd
    - mode: 750

# Prepare API Storage path
# This path needs to be setup before jenkins build. So we can't use jenkins to
# deploy to this path. So it is kind of a "manual work" right now to prepare
# this path
{% if salt['pillar.get']('app:papi:api_storage_path',[]) %}
Git Clone local dev PAPI Code:
  git.latest:
    - name: git@github.com:jobstreet/papi.git
    - target: /tmp/papi
    - identity: /tmp/cijs_rsa
    - user: lighttpd
    - depth: 1 # Depth = 1 because we don't care about history here
    - force_reset: True

{% endif %}

{% for storage_path in salt['pillar.get']('app:papi:api_storage_path',[]) %}
{% if not salt['file.directory_exists' ](storage_path) %}
Prepare PAPI storage directory {{ storage_path }}:
  file.directory:
    - name: {{ storage_path }}
    - makedirs: True

Copy initial PAPI app storage contents to {{ storage_path }}:
  cmd.run:
    - name: rsync -av /tmp/papi/app/storage/ {{ storage_path }}

Set permission for PAPI storage directory {{ storage_path }}:
  file.directory:
    - name: {{ storage_path }}
    - user: lighttpd
    - group: lighttpd
    - mode: 775
    - recurse:
        - user
        - group
        - mode
{% endif %}
{% endfor %}

{% for root_path in salt['pillar.get']('app:papi:api_root_path',[]) %}
Prepare PAPI root directory {{ root_path }}:
  file.directory:
    - name: {{ root_path }}
    - user: lighttpd
    - group: lighttpd
    - mode: 775
    - makedirs: True
{% endfor %}

{% for api_path in salt['pillar.get']('app:papi:api_path',[]) %}
Prepare PAPI api directory {{ api_path }}:
  file.directory:
    - name: {{ api_path }}
    - user: lighttpd
    - group: lighttpd
    - mode: 775
    - makedirs: True
{% endfor %}

Prepare Monitor root directory:
  file.directory:
    - name: /home/lighttpd/monitor-root
    - user: lighttpd
    - group: lighttpd
    - mode: 775
    - makedirs: True

Setup purge log script:
  file.managed:
    - name: /home/lighttpd/purge-api-logs.sh
    - source: salt://app/papi/purge-api-logs.sh
    - template: jinja

Prepare Memcached log directory:
  file.directory:
    - name: /var/log/memcached
    - user: root
    - group: root
    - mode: 755
    - makedirs: True

Setup Memcached monitoring script:
  file.managed:
    - name: /root/check_memcached.sh
    - source: salt://app/papi/check_memcached.sh

Create crontab file if not exist:
  file.managed:
    - name: /root/crontab.txt

Insert purge script into crontab:
  file.blockreplace:
    - name: /root/crontab.txt
    - marker_start: "# START saltstack generated crontab entries (for papi)"
    - marker_end:   "# END   saltstack generated crontab entries (for papi)"
    - content: |
        0 0 * * * sh /home/lighttpd/purge-api-logs.sh
        */1 * * * * sh /root/check_memcached.sh > /var/log/memcached/memcached-monitor.log 2>&1
    - backup: '.bak'
    - append_if_not_found: True

  cmd.run:
    - name: crontab /root/crontab.txt

